PROJECTENGRAM

EDA

Tags: #◇CSMODEL #◇EDA #◇Computer Science #◇Data Science #◇Statistical Modeling #◇Python #◇Machine Learning


Module 06: Exploratory Data Analysis

◇Key Concepts

1. Introduction to Exploratory Data Analysis (EDA)

Exploratory Data Analysis (EDA) is an approach to analyzing data sets to summarize their main characteristics, often using visual methods and summary statistics. It allows data scientists to get a first look at the data, understand its structure, detect anomalies/outliers, test assumptions, and form hypotheses for inferential or predictive modeling.

EDA is anchored on three main pillars:

  1. Statistics: Measures of central tendency, dispersion, and distribution shape.
  2. Data Cleaning: Identifying inconsistencies, duplicate records, missing values, and formatting errors.
  3. Visualization: Graphical representations (histograms, box plots, scatterplots, heatmaps) to uncover underlying patterns.

Spectrum of Data Analysis Questions Descriptive: Summarizes features of a dataset without interpretation or extrapolation. Exploratory: Discovers connections, patterns, or relationships to generate hypotheses. Inferential: Tests whether patterns observed in a sample hold true for a broader population. Predictive: Uses historical data to predict future individual observations. Causal: Evaluates whether changing one variable changes another on average. Mechanistic: Determines deterministic exact physical/biological mechanisms causing changes.


2. Measures of Central Tendency

Measures of central tendency quantify the "center" or typical value of a probability distribution or dataset.

A. Arithmetic Mean (μ\mu or xˉ\bar{x})

The sum of all values divided by the total number of observations. Formula:
xˉ=∑i=1nxin\bar{x} = \frac{\sum_{i=1}^{n} x_i}{n} When to use: Continuous or discrete numerical data that is approximately symmetric. Pros & Cons: Uses every data point; highly sensitive to extreme values (outliers). Cannot be computed for nominal categorical data.

B. Trimmed Mean

The mean calculated after removing a top and bottom percentage (p%p\%) of extreme values. Example: A 12% trimmed mean on 15 values removes the 2 highest and 2 lowest values (15×0.12=1.8≈215 \times 0.12 = 1.8 \approx 2). When to use: Numerical data with suspected outliers or heavy tails. Pros & Cons: Combines the efficiency of the mean with the robustness of the median.

C. Median

The middle observation in an ordered dataset (or average of the two middle values if nn is even). When to use: Skewed numerical data or ordinal categorical data. Pros & Cons: Robust against extreme outliers. Does not take into account the exact magnitude of values outside the center.

D. Mode

The most frequently occurring value in the dataset. When to use: Categorical data (nominal or ordinal) and discrete numerical distributions. Pros & Cons: Works for non-numerical data. May not represent the distribution center well if multimodal or uniform (no mode).

MeasureData TypeOutlier SensitivityUnique Value?
MeanNumeric (Interval/Ratio)High (Sensitive)Yes
Trimmed MeanNumeric (Interval/Ratio)Moderate (Robust)Yes
MedianNumeric & OrdinalLow (Robust)Yes
ModeNominal, Ordinal, NumericNoneNo (Bimodal/Multimodal/None)

3. Measures of Spread & Dispersion

Dispersion quantifies the variability, spread, or scatter of data points around the central tendency.

A. Range

The numerical difference between the maximum and minimum observations. Formula: Range=Xmax−Xmin\text{Range} = X_{\text{max}} - X_{\text{min}} Pros & Cons: Simple to compute; extremely sensitive to outliers as it only considers two observations.

B. Interquartile Range (IQR)

The range covered by the middle 50% of the data. Formula: IQR=Q3−Q1\text{IQR} = Q_3 - Q_1
where Q1Q_1 is the 25th percentile (first quartile) and Q3Q_3 is the 75th percentile (third quartile). Pros & Cons: Highly robust against extreme outliers; useful for skewed data.

C. Variance (s2s^2 or σ2\sigma^2)

The average of the squared deviations from the mean. Sample Variance Formula (s2s^2):
s2=∑i=1n(xi−xˉ)2n−1s^2 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1} Population Variance Formula (σ2\sigma^2):
σ2=∑i=1N(xi−μ)2N\sigma^2 = \frac{\sum_{i=1}^{N} (x_i - \mu)^2}{N}

D. Standard Deviation (ss or σ\sigma)

The square root of the variance, expressed in the same units as the original data. Formula: s=s2s = \sqrt{s^2} Pros & Cons: Incorporates all observations; sensitive to extreme values/outliers.


4. Shape of Distribution: Skewness and Kurtosis

A. Skewness

Skewness measures the degree of asymmetry of a distribution around its mean. Symmetric Distribution: Mean≈Median≈Mode\text{Mean} \approx \text{Median} \approx \text{Mode}. Skewness =0= 0. Positively Skewed (Right-Skewed): Right tail is longer/fatter. Mean>Median>Mode\text{Mean} > \text{Median} > \text{Mode}. Skewness >0> 0. Negatively Skewed (Left-Skewed): Left tail is longer/fatter. Mean<Median<Mode\text{Mean} < \text{Median} < \text{Mode}. Skewness <0< 0.

B. Kurtosis

Kurtosis measures the "tailedness" and sharpness of the peak relative to a standard normal distribution. Mesokurtic (Kurtosis≈0\text{Kurtosis} \approx 0 excess): Normal distribution shape. Leptokurtic (Kurtosis>0\text{Kurtosis} > 0 excess): High sharp peak, fat/heavy tails (higher likelihood of extreme outliers). Platykurtic (Kurtosis<0\text{Kurtosis} < 0 excess): Flat peak, light/thin tails.


5. Univariate Analysis

Univariate analysis examines one variable at a time to inspect its distribution and statistics.
Dot Plot: Plots individual observations along a single axis. Best for small numerical datasets. Histogram: Groups continuous observations into discrete ranges (bins) to show density and identify modes. Bar Plot: Uses rectangular bars proportional to frequencies or aggregated values across discrete categorical groups. Box Plot (Box-and-Whisker Plot): Visual representation of the Five-Number Summary:

  1. Minimum / Lower Whisker boundary: Q1−1.5×IQRQ_1 - 1.5 \times \text{IQR} (or lowest data point within this limit)
  2. First Quartile (Q1Q_1)
  3. Median (Q2Q_2)
  4. Third Quartile (Q3Q_3)
  5. Maximum / Upper Whisker boundary: Q3+1.5×IQRQ_3 + 1.5 \times \text{IQR} (or highest data point within this limit)
    Data points beyond the whiskers are flagged as suspected outliers.

6. Bivariate & Multivariate Analysis

A. Scatter Plot

Visualizes paired numerical data (xi,yi)(x_i, y_i) to identify linear, non-linear, or zero correlation between two variables.

B. Correlation Coefficients

Correlation measures the strength and direction of a relationship between two variables, bounded in [−1,1][-1, 1].

  1. Pearson Correlation Coefficient (rxyr_{xy}):
    Assesses the linear relationship between continuous interval or ratio variables.
    Assumes normal distribution of variables and linear trend.
    Formula:
    rxy=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2∑i=1n(yi−yˉ)2r_{xy} = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=1}^{n} (x_i - \bar{x})^2} \sqrt{\sum_{i=1}^{n} (y_i - \bar{y})^2}}

  2. Spearman Rank Correlation Coefficient (rsr_s or ρ\rho):
    Assesses the monotonic relationship (whether linear or non-linear) between two variables using ranked values.
    Non-parametric: does not require normal distribution or continuous interval scale (works on ordinal data).
    Formula (when no ties exist):
    rs=1−6∑i=1ndi2n(n2−1)r_s = 1 - \frac{6 \sum_{i=1}^{n} d_i^2}{n(n^2 - 1)}
    where di=rank(xi)−rank(yi)d_i = \text{rank}(x_i) - \text{rank}(y_i).

Comparison: Pearson vs. Spearman

FeaturePearson CorrelationSpearman Correlation
Relationship TypeStrict Linear RelationshipMonotonic (Linear or Non-Linear)
Data TypesContinuous Interval / RatioContinuous or Ordinal Ranks
Normality AssumptionRequires normal distributionNon-parametric (No distribution assumption)
Outlier SensitivityHighLow (Robust because ranks are used)

C. Multivariate Visualizations Heatmap: Displays a matrix of values (e.g., correlation matrix) using color gradients for quick pattern recognition. Pair Plot: Grid of pairwise scatter plots for multiple numerical variables with histograms/KDEs along the diagonal.


7. Grouping & Aggregation

Grouping collapses data across categorical factors using a split-apply-combine strategy:

  1. Split: Break data into groups based on key values (groupby('type_1')).
  2. Apply: Compute statistical metrics (mean, std, median, count) on numerical target columns.
  3. Combine: Aggregate results into a summary DataFrame.

◇Python Snippets

1. pandas.DataFrame.describe()

Generates summary statistics of numeric columns in a DataFrame (count, mean, std, min, 25%, 50%, 75%, max). Parameters: include='all' (includes categorical columns), percentiles=[.25, .5, .75]. Returns: DataFrame containing summary statistics.

『』
PYTHON
import pandas as pd

# Read dataset
pokemon_df = pd.read_csv('pokemon.csv')

# Display summary statistics of numeric columns
stats_df = pokemon_df[['hp', 'attack', 'defense', 'speed]].describe()
print(stats_df)

2. Summary Statistics Functions (mean, median, std, var, skew, kurt)

Computes univariate summary statistics across Series or DataFrame columns. Parameters: axis=0 (columns) or 1 (rows), numeric_only=True, ddof=1 (delta degrees of freedom for sample variance/std). Returns: Scalar value (Series) or Series (DataFrame).

『』
PYTHON
# Compute individual summary statistics for HP column
hp_mean = pokemon_df['hp'].mean()
hp_median = pokemon_df['hp'].median()
hp_std = pokemon_df['hp'].std()      # Sample standard deviation (ddof=1 by default)
hp_var = pokemon_df['hp'].var()      # Sample variance
hp_skew = pokemon_df['hp'].skew()    # Positive > 0, Negative < 0
hp_kurt = pokemon_df['hp'].kurt()    # Excess kurtosis

print(f"Mean: {hp_mean:.2f}, Median: {hp_median:.2f}, Skew: {hp_skew:.2f}")

3. pandas.DataFrame.corr()

Computes pairwise correlation matrix of columns. Parameters: method='pearson' (default) or 'spearman', numeric_only=True. Returns: Square DataFrame of correlation coefficients ranging from -1.0 to 1.0.

『』
PYTHON
# Pearson correlation matrix (linear)
pearson_corr = pokemon_df[['hp', 'attack', 'defense]].corr(method='pearson')

# Spearman correlation matrix (monotonic/rank)
spearman_corr = pokemon_df[['hp', 'attack', 'defense]].corr(method='spearman')

print("Pearson Correlation:")
print(pearson_corr)
print("\nSpearman Correlation:")
print(spearman_corr)

4. pandas.DataFrame.groupby() & pandas.DataFrame.agg()

Groups data by categorical column(s) and applies aggregation functions. Parameters: groupby(by='column_name'), agg({'target_col': ['mean', 'std', 'median']}). Returns: Grouped DataFrame with simple or hierarchical (MultiIndex) columns.

『』
PYTHON
# Group by primary type and compute mean & std of Attack
attack_stats = pokemon_df.groupby('type_1').agg({'attack': ['mean', 'std', 'median']})

# Sort by mean attack descending (using tuple for MultiIndex column)
sorted_attack = attack_stats.sort_values(('attack', 'mean'), ascending=False)
print(sorted_attack.head(5))

5. Matplotlib Plotting (plt.hist, plt.boxplot, plt.scatter)

Renders basic univariate and bivariate plots.

『』
PYTHON
import matplotlib.pyplot as plt

plt.style.use('seaborn-v0_8-darkgrid')

# 1. Histogram of Weight
plt.figure(figsize=(7, 4))
plt.hist(pokemon_df['weight_kg'].dropna(), bins=30, edgecolor='white', color='skyblue')
plt.title('Weight Distribution of Pokemon')
plt.xlabel('Weight (kg)')
plt.ylabel('Frequency')
plt.show()

# 2. Side-by-side Boxplot of Attack by Primary Type
pokemon_df.boxplot(column='attack', by='type_1', figsize=(12, 6))
plt.title('Attack by Primary Type')
plt.suptitle('')
plt.xlabel('Primary Type')
plt.ylabel('Attack Point')
plt.xticks(rotation=45)
plt.show()

# 3. Scatter plot of HP vs Attack
plt.figure(figsize=(7, 4))
plt.scatter(pokemon_df['hp'], pokemon_df['attack'], alpha=0.6, c='coral', edgecolors='k')
plt.title('HP vs Attack')
plt.xlabel('HP')
plt.ylabel('Attack')
plt.show()

6. Seaborn Visualizations (sns.histplot, sns.boxplot, sns.scatterplot, sns.heatmap, sns.pairplot, sns.barplot)

Renders high-level statistical data plots with enhanced styling.

『』
PYTHON
import seaborn as sns
import matplotlib.pyplot as plt

# 1. Seaborn Histplot with KDE
sns.histplot(data=pokemon_df, x='hp', kde=True, bins=20, color='teal')
plt.title('HP Distribution')
plt.show()

# 2. Seaborn Boxplot
sns.boxplot(data=pokemon_df, x='type_1', y='defense')
plt.xticks(rotation=90)
plt.title('Defense Distribution per Type')
plt.show()

# 3. Seaborn Scatterplot with Hue
sns.scatterplot(data=pokemon_df, x='attack', y='defense', hue='status')
plt.title('Attack vs Defense by Status')
plt.show()

# 4. Correlation Heatmap
corr_matrix = pokemon_df[['hp', 'attack', 'defense', 'sp_attack', 'sp_defense', 'speed]].corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('Pokemon Base Stats Correlation Matrix')
plt.show()

# 5. Pairplot for Multivariate Analysis
sns.pairplot(pokemon_df[['hp', 'attack', 'defense', 'status]], hue='status')
plt.show()

# 6. Barplot for Group Comparisons
sns.barplot(data=pokemon_df, x='type_1', y='attack', estimator='mean', errorbar=None)
plt.xticks(rotation=90)
plt.title('Mean Attack per Primary Type')
plt.show()

◇Formulas & Key Equations

Central Tendency Sample Mean (xˉ\bar{x}):

xˉ=∑xin\bar{x} = \frac{\sum x_i}{n} Trimmed Mean (kk observations trimmed from each end):
xˉtrimmed=∑i=k+1n−kx(i)n−2k\bar{x}_{\text{trimmed}} = \frac{\sum_{i=k+1}^{n-k} x_{(i)}}{n - 2k}

Dispersion Sample Variance (s2s^2):

s2=∑i=1n(xi−xˉ)2n−1s^2 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1} Sample Standard Deviation (ss):
s=∑i=1n(xi−xˉ)2n−1s = \sqrt{\frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1}} Range:
Range=xmax−xmin\text{Range} = x_{\text{max}} - x_{\text{min}} Interquartile Range (IQR):
IQR=Q3−Q1\text{IQR} = Q_3 - Q_1

Outlier Limits (Boxplot Rule) Lower Outlier Boundary:

Lower Limit=Q1−1.5×IQR\text{Lower Limit} = Q_1 - 1.5 \times \text{IQR} Upper Outlier Boundary:
Upper Limit=Q3+1.5×IQR\text{Upper Limit} = Q_3 + 1.5 \times \text{IQR}

Correlation Coefficients Pearson Correlation (rr):

r=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2∑(yi−yˉ)2=cov(x,y)sxsyr = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2} \sqrt{\sum (y_i - \bar{y})^2}} = \frac{\text{cov}(x, y)}{s_x s_y} Spearman Rank Correlation (rsr_s):
rs=1−6∑di2n(n2−1)r_s = 1 - \frac{6 \sum d_i^2}{n(n^2 - 1)}


◇Common Exam Traps

  1. Mean vs. Median in Skewed Distributions:
    In a positively skewed (right-skewed) distribution, the mean is pulled towards the high values in the right tail: Mean>Median>Mode\text{Mean} > \text{Median} > \text{Mode}.
    In a negatively skewed (left-skewed) distribution, the mean is pulled towards the low values in the left tail: Mean<Median<Mode\text{Mean} < \text{Median} < \text{Mode}.
    Exam Trap: Students often confuse the tail direction with the peak direction. Remember: Skewness refers to the direction of the tail.

  2. Pearson vs. Spearman Correlation Assumptions:
    Pearson assumes linear relationship and normally distributed continuous variables. It is sensitive to extreme outliers.
    Spearman evaluates monotonic relationships (increasing/decreasing trends, linear or non-linear) and works on ordinal ranks. It is robust to outliers.
    Exam Trap: Asking for correlation on non-linear monotonic data (e.g. y=x3y = x^3). Pearson will be <1.0< 1.0, while Spearman rank correlation will be exactly 1.01.0!

  3. Boxplot Whisker Boundaries vs. Actual Data Points:
    The lower whisker limit is defined as Q1−1.5×IQRQ_1 - 1.5 \times \text{IQR} and upper whisker as Q3+1.5×IQRQ_3 + 1.5 \times \text{IQR}.
    Exam Trap: The whisker line itself in software plots extends to the lowest/highest actual observation inside those limits, NOT necessarily the mathematical limit calculation.

  4. Histogram vs. Bar Plot Distinctions:
    Histograms display continuous quantitative variables grouped into adjacent numeric bins (no gaps between bars unless a bin has 0 count).
    Bar plots display categorical variables or separate group aggregations (bars are separated by visual gaps).

  5. pandas std() vs NumPy std() Degrees of Freedom:
    pandas.Series.std() defaults to sample standard deviation (ddof=1, dividing by n−1n-1).
    numpy.std() defaults to population standard deviation (ddof=0, dividing by NN).
    Exam Trap: Comparing manual standard deviation formulas with pandas output without accounting for ddof.

  6. Hierarchical MultiIndex Columns in Groupby Aggregations:
    When running df.groupby('type_1').agg({'attack': ['mean', 'std']}), the columns become a MultiIndex.
    Exam Trap: Sorting requires indexing with a tuple: df.sort_values(('attack', 'mean')). Sorting by string 'mean' or 'attack' causes a KeyError.


◇Practice Questions

Q1. A dataset contains the following 10 values of Pokemon height (in meters): [0.3, 0.5, 0.6, 0.6, 1.0, 1.1, 1.5, 2.0, 3.0, 19.4]. What are the median and the interquartile range (IQR) of this dataset? A) Median = 1.05, IQR = 1.40 B) Median = 1.05, IQR = 1.20 C) Median = 1.10, IQR = 1.40 D) Median = 0.85, IQR = 0.90


Q2. Consider the following Python code snippet:

『』
PYTHON
import pandas as pd

data = {'Type': ['Fire', 'Fire', 'Water', 'Water', 'Water'],
        'Attack': [52, 64, 48, 63, 83]}
df = pd.DataFrame(data)

res = df.groupby('Type').agg({'Attack': ['mean', 'median']})
print(res.loc['Water', ('Attack', 'median')])

What is the output of the print statement? A) 64.67 B) 63.0 C) 58.0 D) 48.0


Q3. An analyst computes both the Pearson correlation coefficient (rr) and the Spearman rank correlation coefficient (rsr_s) for two continuous variables XX and YY. The scatter plot shows a perfect monotonic curve (y=exy = e^x), where YY strictly increases as XX increases, but the relationship is non-linear. What can be concluded about the values of rr and rsr_s? A) r=1.0r = 1.0 and rs=1.0r_s = 1.0 B) r<1.0r < 1.0 and rs=1.0r_s = 1.0 C) r=1.0r = 1.0 and rs<1.0r_s < 1.0 D) r<0r < 0 and rs>0r_s > 0


Q4. Consider the following Python code snippet using pandas:

『』
PYTHON
import pandas as pd

vals = [10, 20, 30, 40, 250]
s = pd.Series(vals)
print(s.skew() > 0, s.mean() > s.median())

What does this code print? A) True True B) False False C) True False D) False True


Q5. In a standard boxplot with Q1=30Q_1 = 30 and Q3=70Q_3 = 70, which of the following data points in a dataset would be flagged as a suspected outlier? A) 10 B) -35 C) 120 D) Both B and C


Q6. A data scientist wants to compare the frequency distribution of categorical Pokemon primary types (type_1) across generations. Which visualization tool is most appropriate for comparing these categorical group frequencies side-by-side? A) Scatter plot B) Grouped Bar plot C) Histogram D) Line plot


Answer Key

Q1: A —
Ordered data (n=10n=10): [0.3, 0.5, 0.6, 0.6, 1.0, 1.1, 1.5, 2.0, 3.0, 19.4]
Median = average of 5th and 6th values = 1.0+1.12=1.05\frac{1.0 + 1.1}{2} = 1.05.
Q1Q_1 (median of lower 5 values [0.3, 0.5, 0.6, 0.6, 1.0]) = 0.6.
Q3Q_3 (median of upper 5 values [1.1, 1.5, 2.0, 3.0, 19.4]) = 2.0.
IQR=Q3−Q1=2.0−0.6=1.40\text{IQR} = Q_3 - Q_1 = 2.0 - 0.6 = 1.40.
Q2: B —
For Water attack values [48, 63, 83], the sorted values are [48, 63, 83].
The median value is 63.0.
Indexing the MultiIndex column ('Attack', 'median') for row 'Water' retrieves 63.0.
Q3: B —
Spearman rank correlation measures monotonic relationships. Since y=exy=e^x is strictly increasing, ranks match perfectly, so rs=1.0r_s = 1.0.
Pearson correlation measures linear relationships. Because y=exy=e^x is non-linear, r<1.0r < 1.0.
Q4: A —
The single high value 250 creates a long right tail, resulting in a positively skewed distribution (skew>0  ⟹  True\text{skew} > 0 \implies \text{True}).
Mean = 10+20+30+40+2505=3505=70.0\frac{10+20+30+40+250}{5} = \frac{350}{5} = 70.0.
Median = 30.0. Since Mean(70.0)>Median(30.0)\text{Mean} (70.0) > \text{Median} (30.0), s.mean() > s.median() evaluates to True.
Q5: B —
IQR=Q3−Q1=70−30=40\text{IQR} = Q_3 - Q_1 = 70 - 30 = 40.
Lower Limit = Q1−1.5×IQR=30−1.5(40)=30−60=−30Q_1 - 1.5 \times \text{IQR} = 30 - 1.5(40) = 30 - 60 = -30.
Upper Limit = Q3+1.5×IQR=70+1.5(40)=70+60=130Q_3 + 1.5 \times \text{IQR} = 70 + 1.5(40) = 70 + 60 = 130.
Data point -35 is below −30-30, so -35 is outside the lower whisker limit and flagged as a suspected outlier. Data point 120 is within the upper limit (120≤130120 \le 130).
Q6: B —
Categorical group comparisons are best visualized using a Bar plot (specifically a grouped bar plot across generations). Histograms are for continuous numeric data; scatter plots are for bivariate continuous data.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Which measure of central tendency is most robust to extreme outliers?

Q2

What does positive skewness indicate about a distribution?

Q3

Kurtosis measures:

Q4

A Pearson correlation coefficient of -0.9 implies:

Q5

In descriptive statistics, standard deviation represents:

EDA | PROJECT ENGRAM